﻿using System;
using System.Windows.Media.Imaging;
using SomethingSpacial.CustomControls;
using SomethingSpacial.Model;
using SomethingSpacialClassLib.Web;
using System.Collections.ObjectModel;
using System.ServiceModel.DomainServices.Client;

namespace SomethingSpacial.ViewModel
{
    public class LocationsViewModel : ViewModelBase
    {
        private LocationModel MyModel = new LocationModel();
        private SomethingSpacialContext domainContext;
        private SomethingSpacialContext DomainContext
        {
            get
            {
                if (domainContext == null)
                    domainContext = new SomethingSpacialContext();
                return domainContext;
            }
        }
        LoadOperation LoadLocations;

        public EntitySet<SomethingSpacialClassLib.Web.Location> Location
        {
            get
            {
                return (EntitySet<SomethingSpacialClassLib.Web.Location>)DomainContext.Locations;
            }
            //No set, it is a read-only property.
        }


        #region "ViewModel Methods"

        public void loadLocations()
        {
            LoadLocations = DomainContext.Load(DomainContext.GetLocationsQuery());            
        }

        public void AddLocation(Location locationNew)
        {
            try
            {
                DomainContext.Locations.Add(locationNew);
                DomainContext.SubmitChanges();
            }
            catch (Exception exc)
            {
                string message = exc.InnerException.ToString();
                System.Windows.MessageBox.Show(message);
            }
        }

          public void UpdateLocation()
        {
           DomainContext.SubmitChanges();
        }

          public void DeleteLocation(Location location)
          {
              try
              {
                  DomainContext.Locations.Remove(location);
                  DomainContext.SubmitChanges();
              }
              catch (Exception exc)
              {
                  string message = exc.InnerException.ToString();
                  System.Windows.MessageBox.Show(message);
              }

          }   


        #endregion

        #region "ViewModel Properties"

        public int LocationID
        {
            get { return MyModel.LocationID; }
            set
            {
                if (MyModel.LocationID != value)
                {
                    MyModel.LocationID = value;
                    InvokePropertyChanged("LocationID");
                }
            }
        }


        public string LocationName
        {
            get { return MyModel.LocationName; }
            set
            {
                if (MyModel.LocationName != value)
                {
                    MyModel.LocationName = value;
                    InvokePropertyChanged("LocationName");
                }
            }
        }


        public string StreetAddress1
        {
            get { return MyModel.StreetAddress1; }
            set
            {
                if (MyModel.StreetAddress1 != value)
                {
                    MyModel.StreetAddress1 = value;
                    InvokePropertyChanged("StreetAddress1");
                }
            }
        }

        public string StreetAddress2
        {
            get { return MyModel.StreetAddress2; }
            set
            {
                if (MyModel.StreetAddress2 != value)
                {
                    MyModel.StreetAddress2 = value;
                    InvokePropertyChanged("StreetAddress2");
                }
            }
        }

        public string Comments
        {
            get { return MyModel.Comments; }
            set
            {
                if (MyModel.Comments != value)
                {
                    MyModel.Comments = value;
                    InvokePropertyChanged("Comments");
                }
            }
        }

        public string City
        {
            get { return MyModel.City; }
            set
            {
                if (MyModel.City != value)
                {
                    MyModel.City = value;
                    InvokePropertyChanged("City");
                }
            }
        }

        public string State
        {
            get { return MyModel.State; }
            set
            {
                if (MyModel.State != value)
                {
                    MyModel.State = value;
                    InvokePropertyChanged("State");
                }
            }
        }

        public string PostalCode
        {
            get { return MyModel.PostalCode; }
            set
            {
                if (MyModel.PostalCode != value)
                {
                    MyModel.PostalCode = value;
                    InvokePropertyChanged("PostalCode");
                }
            }
        }


        public string Country
        {
            get { return MyModel.Country; }
            set
            {
                if (MyModel.Country != value)
                {
                    MyModel.Country = value;
                    InvokePropertyChanged("Country");
                }
            }
        }

        public string Lat
        {
            get { return MyModel.Lat; }
            set
            {
                if (MyModel.Lat != value)
                {
                    MyModel.Lat = value;
                    InvokePropertyChanged("Lat");
                }
            }
        }

        public string Long
        {
            get { return MyModel.Long; }
            set
            {
                if (MyModel.Long != value)
                {
                    MyModel.Long = value;
                    InvokePropertyChanged("Long");
                }
            }
        }          
        
        #endregion
    }
}
